package com.yykj.hospital.server.service.consultation.impl;

import com.yykj.common.enums.ResultEnums;
import com.yykj.common.exception.NormalException;
import com.yykj.hospital.client.dto.HospitalSettingDTO;
import com.yykj.hospital.client.dto.consultation.template.ConsultationVerifyDTO;
import com.yykj.hospital.client.form.consultation.ConsultationVerifyForm;
import com.yykj.hospital.server.model.entity.HospitalDoctor;
import com.yykj.hospital.server.model.repository.HospitalDoctorRepository;
import com.yykj.hospital.server.service.HospitalService;
import com.yykj.hospital.server.service.consultation.HospitalConsultationVerifyService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class HospitalConsultationVerifyServiceImpl implements HospitalConsultationVerifyService {
    @Override
    public List<ConsultationVerifyDTO> verify(ConsultationVerifyForm form) {
        if (form.getDoctorIds() == null || form.getDoctorIds().size() == 0) {
            throw new NormalException(ResultEnums.PARAM_ERROR);
        }
        List<HospitalDoctor> hospitalDoctorList = hospitalDoctorRepository.findAllByDoctorIdIn(form.getDoctorIds());
        Map<Integer, HospitalSettingDTO> hospitalSettingMap = new HashMap<>();
        Map<String,HospitalDoctor>  doctorMap = new HashMap<>();
        for (HospitalDoctor doctor:hospitalDoctorList){
            doctorMap.put(doctor.getDoctorId(),doctor);
            HospitalSettingDTO hospitalSetting = hospitalSettingMap.get(doctor.getHospitalId());
            if(hospitalSetting == null){
                hospitalSetting = hospitalService.getHospitalSetting(doctor.getHospitalId());
                hospitalSettingMap.put(doctor.getHospitalId(),hospitalSetting);
            }
        }
        List<ConsultationVerifyDTO> result = new ArrayList<>();
        for(String doctorId: form.getDoctorIds()){
            ConsultationVerifyDTO dto = new ConsultationVerifyDTO();
            HospitalDoctor doctor = doctorMap.get(doctorId);
            if(doctor == null){
                throw new NormalException(ResultEnums.NOT_EXIST,doctorId +"不存在");
            }
            HospitalSettingDTO hospitalSettingDTO = hospitalSettingMap.get(doctor.getHospitalId());
            BeanUtils.copyProperties(hospitalSettingDTO,dto);
            BeanUtils.copyProperties(doctor,dto);
            dto.setDoctorId(doctorId);
            dto.setConsultationId(form.getConsultationId());
            result.add(dto);
        }
        return result;
    }

    private HospitalService hospitalService;

    private HospitalDoctorRepository hospitalDoctorRepository;

    @Autowired
    public void setHospitalService(HospitalService hospitalService) {
        this.hospitalService = hospitalService;
    }

    @Autowired
    public void setHospitalDoctorRepository(HospitalDoctorRepository hospitalDoctorRepository) {
        this.hospitalDoctorRepository = hospitalDoctorRepository;
    }
}
